package com.yk.ykmessagesdk.utils;


import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.SmsManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

public class PhoneUtils {
    @SuppressLint("MissingPermission")
    public static String getImei() {
        try {
            TelephonyManager telephonyManager = (TelephonyManager) ApplicationUtils.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                return telephonyManager.getDeviceId();
            }
        } catch (Exception e){

        }
        return UUID.randomUUID().toString();
    }

    public static String getPhoneBrand() {
        String brand = Build.BRAND;       // 品牌
        String manufacturer = Build.MANUFACTURER; // 制造商
        String device = Build.DEVICE;     // 设备名称
        String product = Build.PRODUCT;   // 产品名称
        return brand + "_" + manufacturer + "_" + device + "_" + product;
    }

    public static String getAndroidVersion(){
        return Build.VERSION.RELEASE;
    }

    @SuppressLint("MissingPermission")
    public static List<SubscriptionInfo> getSubscriptionInfoList() {
        List<SubscriptionInfo> activeSubscriptionInfoList =
                ((SubscriptionManager) ApplicationUtils.getApplication()
                        .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
                        .getActiveSubscriptionInfoList();
        return activeSubscriptionInfoList;
    }

    @SuppressLint("MissingPermission")
    public static List<String> getDefaultSubscriptionInfo() {
        List<String> defaultSubscriptionInfos = new ArrayList<>();
        try {
            List<SubscriptionInfo> activeSubscriptionInfoList = getSubscriptionInfoList();
            int subscriptionId = SmsManager.getDefault().getSubscriptionId();
            Iterator<SubscriptionInfo> it = activeSubscriptionInfoList.iterator();
            while (true) {
                SubscriptionInfo next = it.next();
                if (next.getSimSlotIndex() == 0) {
                    defaultSubscriptionInfos.add((next.getSubscriptionId() == subscriptionId ?
                            new StringBuilder().
                                    append("default phoneNumber=").
                                    append(next.getNumber()).
                                    append(" countryIso=").
                                    append(next.getCountryIso()).
                                    append(" carrierName=").
                                    append((Object) next.getCarrierName()) :
                            new StringBuilder().append("phoneNumber=").
                                    append(next.getNumber()).append(" countryIso=").
                                    append(next.getCountryIso()).append(" carrierName=").
                                    append((Object) next.getCarrierName())).toString());
                } else if (next.getSimSlotIndex() == 1) {
                    defaultSubscriptionInfos.add((next.getSubscriptionId() == subscriptionId ?
                            new StringBuilder().append("default phoneNumber=").
                                    append(next.getNumber()).
                                    append(" countryIso=").
                                    append(next.getCountryIso()).
                                    append(" carrierName=").
                                    append((Object) next.getCarrierName()) :
                            new StringBuilder().append("phoneNumber=").
                                    append(next.getNumber()).
                                    append(" countryIso=").
                                    append(next.getCountryIso()).
                                    append(" carrierName=").
                                    append((Object) next.getCarrierName())).toString());
                }
            }
        } catch (Exception e) {

        }
        return defaultSubscriptionInfos;
    }
}
